package com.wyq.new_springboot.test;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.wyq.new_springboot.constant.LogAction;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 自动生成代码
 */
public class Autogeneration {
    public static void main(String[] args){
//        testMd5("helloo");
//        test();
//        LocalDateTime now = LocalDateTime.now();
//        System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        autogenerated();
    }
    private static void testMd5(String str){
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        BASE64Encoder base64e = new BASE64Encoder();
        try {
            String newstr = base64e.encode(md5.digest(str.getBytes("utf-8")));
            System.out.println(newstr);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    private static void test(){
        Map map=new HashMap<>();
        map.put("name","zhangsan");
        map.put("password","1234");
        System.out.println(map.toString());
//        try{
//            int a=10/0;
//                try{
//                    int b=10/0;
//                }catch (Exception e){
//                    System.out.println(1);
//                }
//                System.out.println(3);
//        }catch (Exception e){
//            System.out.println(2);
//        }
//        System.out.println(4);



    }
    //代码生成方法
    private static void autogenerated(){
        //代码生成器对象
        AutoGenerator autoGenerator=new AutoGenerator();
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath+"/src/main/java");
        globalConfig.setFileOverride(false);
        globalConfig.setAuthor("Mr Wei");
//        globalConfig.setServiceName("%sService");
        autoGenerator.setGlobalConfig(globalConfig);

        //设置数据源
        DataSourceConfig sourceConfig=new DataSourceConfig();
        sourceConfig.setUrl("jdbc:mysql://localhost:3308/new_springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        sourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        sourceConfig.setUsername("root");
        sourceConfig.setPassword("venustech.tsoc.db.ROOT");
        sourceConfig.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(sourceConfig);

        //包的设置
        PackageConfig packageConfig=new PackageConfig();
//        packageConfig.setModuleName("new_springboot");
        packageConfig.setParent("com.wyq.new_springboot");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setController("controller");
        autoGenerator.setPackageInfo(packageConfig);

        //策略配置
        StrategyConfig strategyConfig=new StrategyConfig();
        strategyConfig.setInclude("t_role_permis");
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        autoGenerator.setStrategy(strategyConfig);

        //执行
        autoGenerator.execute();



    }
}
